package com.jzo2o.market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.market.model.dto.request.ActivityQueryForPageReqDTO;
import com.jzo2o.market.model.dto.request.ActivitySaveReqDTO;
import com.jzo2o.market.model.dto.response.ActivityInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/operation/activity")
@Slf4j
@Api(tags = "优惠券相关接口")
public class MarketController {

    @Autowired
    private IActivityService activityService;

    @PostMapping("/save")
    @ApiOperation("保存优惠券")
    public void saveActivity(@RequestBody ActivitySaveReqDTO activitySaveReqDTO){
        activityService.saveActivity(activitySaveReqDTO);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询优惠券")
    public PageResult<ActivityInfoResDTO> pageActivity(ActivityQueryForPageReqDTO activityQueryForPageReqDTO){
        IPage<ActivityInfoResDTO> activityInfoResDTOIPage = activityService.queryActivityForPage(activityQueryForPageReqDTO);
        // 将 IPage 转换为 PageResult
        PageResult<ActivityInfoResDTO> pageResult = new PageResult<>();
        pageResult.setPages(activityInfoResDTOIPage.getPages());
        pageResult.setTotal(activityInfoResDTOIPage.getTotal());
        pageResult.setList(activityInfoResDTOIPage.getRecords());
        return pageResult;
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询优惠券")
    public ActivityInfoResDTO getActivityById(@PathVariable("id") Long id){
        return activityService.getActivityById(id);
    }

    @PostMapping("/revoke/{id}")
    public void revokeActivity(@PathVariable("id") Long id){
        activityService.revokeActivity(id);
    }
}
